import time

from selenium import webdriver


def userlistprinter(func):
    def gg(*args, **kwargs):
        with open('./data.txt', mode='r', encoding='utf-8')as f:
            listf = f.readlines()
            for i in range(len(listf)):
                name = listf[i].strip('\n').split(',')[0]
                passwd = listf[i].strip('\n').split(',')[1]
                func(*args, name, passwd)

        # print("hello gg")

    return gg


def choosebrowser(func):
    def chooser(*args, **kwargs):
        bname='safari'
        func(*args, bname)
        print("browser :%s has been choosed"%bname)

    return chooser


def openwebsite(func):
    def websites(*args,**kwargs):
        func(*args,"http://www.baidu.com")
        print("let me check your website status")

    return websites


class Bddsuit():
    def __init__(self):
        # ws=webdriver.Safari()
        # ws.get('http://www.baidu.com')
        # time.sleep(2)

        pass

    @choosebrowser
    def step_do_browser(self, browsername):
        print("which browser should i choose?")
        if browsername == "safari":

            webdriver.Safari().quit()
            self.ws = webdriver.Safari()

        elif browsername == "chorme":
            self.ws = webdriver.Chrome()
        elif browsername == "firefox":
            self.ws = webdriver.Firefox()
        else:
            print("No browser driver be instanced")
        return self.ws

    @openwebsite
    def step_check_status(self,weburl):

        self.ws.get(weburl)
        self.ws.set_page_load_timeout(5)
        # print(self.ws.page_source)
        print('is my website status ok?')
        time.sleep(2)
        self.ws.execute_script('window.open("http://www.zhihu.com")')
        time.sleep(2)
        print(self.ws.title)
        self.ws.switch_to.window(self.ws.window_handles[1])
        time.sleep(1)
        print(self.ws.title)
        time.sleep(10)
        self.ws.quit()

    @userlistprinter
    def userinfo(self, name, paswd):
        print("user info: " + name, paswd)


# def datain():
#     for i in range(20):
#         name='tolle%s'%i
#         passwd="tollepaswd%s"%i
#         with open(file='./data.txt',mode='a')as f:
#             f.write(name+','+passwd+'\n')

if __name__ == '__main__':
    # r=closure()
    # r()

    # do_login(a="myname",b="mypasswd",c="keep-alive")
    bs = Bddsuit()
    # bs.userinfo()
    # bs.step_do_browser()
    # bs.step_check_status()
    print(hasattr(Bddsuit,'userinfo'))
    # datain()
